package com.example.mcpservice.tools;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;

/**
 * 门店服务
 *
 * @author xuguang
 */
@Slf4j
@Service
public class ShopService {

    private final WebClient webClient;

    public ShopService(WebClient.Builder webClientBuilder) {
        this.webClient = webClientBuilder
                .baseUrl("http://localhost:8808/md-shop-manager")
                .build();
    }


    /**
     * pos开关
     */
    @Tool(description = "根据门店id获取对应要求的开关")
    public String queryShopParam(@ToolParam(description = "门店id，例如：1000001") Long shopId) {
        try {
            String response = webClient.post()
                    .uri(uriBuilder -> uriBuilder
                            .path("/channelSwitch/list")
                            .build())
                    .body(BodyInserters.fromValue(new ShopParam(shopId)))
                    .retrieve()
                    .bodyToMono(String.class)
                    .block();

            // 解析响应并返回格式化的天气信息
            return "根据门店id获取对应要求的开关, shopId：" + shopId + ", 结果:"+ response;
        } catch (Exception e) {
            log.error("根据门店id获取对应要求的开关失败异常", e);
            return "根据门店id获取对应要求的开关失败：" + e.getMessage();
        }
    }


    /**
     * pos开关
     */
    @Tool(description = "查询多个门店的详细信息，包括门店图片等，可用于识别特定图片内容的门店")
    public String queryAllShopList(@ToolParam(description = "groupId")String groupId) {
        try {
            String response = webClient.post()
                    .uri(uriBuilder -> uriBuilder
                            .path("/channelSwitch/list")
                            .build())
                    .body(BodyInserters.fromValue(new ShopParam()))
                    .retrieve()
                    .bodyToMono(String.class)
                    .block();

            // 解析响应并返回格式化的天气信息
            return "查询多个门店的详细信息，包括门店图片, 结果:"+ response;
        } catch (Exception e) {
            log.error("查询多个门店的详细信息，包括门店图片", e);
            return "查询多个门店的详细信息，包括门店图片：" + e.getMessage();
        }
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class ShopParam {

        private Long shopId;
    }
}